<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Victor
 * Date: 08.11.11
 * Time: 22:55
 * To change this template use File | Settings | File Templates.
 */

class DataModule
{
    private static $host = 'localhost';
    private static $port = '3306';
    private static $driver = 'mysql';
    /**
     * @var PDO
     */
    public static $connection = null;
    private static $database = 'education';
    private static $instance = null;

    private static $user = 'root';
    private static $password = '535332';

    public static $error_array = array();
     /**
      * @static
      * @return DataModule
      */
    public static function getInstance()
    {
        if (self::$instance == null) {
            self::$instance = new DataModule();
        }
        return self::$instance;
    }

    public function loadDefaultConfig(){
       // echo "load config </br>";
        $this->setDatabase('student2');
        $this->setDriver('mysql');
        $this->setHost('localhost');
        $this->setUser('student');
        $this->setPassword('student');

    }
    /**
     * @return PDO
     */
    public function getConnection(){

           $this->connect();

        return self::$connection;

    }
    public function closeConnection(){
        self::$connection = null;
    }
public function connect(){

    $this->loadDefaultConfig();
    $connect_str = self::$driver . ':host=' . self::$host . ';dbname=' . self::$database.";charset=UTF-8";
    //echo "<pre>connection string is : $connect_str</pre></br>";
    try{
     // echo "go connect</br>";
    self::$connection = new PDO($connect_str,self::$user,self::$password);
    self::$connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    self::$connection -> exec("set names utf8");

    }
    catch(PDOException $e){
       
        echo 'Error: '. $e->getMessage() . '</br>';

    }}


    public function setDatabase($database)
    {
        $this->database = $database;
    }

    public function getDatabase()
    {
        return $this->database;
    }

    public function setDriver($driver)
    {
        $this->driver = $driver;
    }

    public function getDriver()
    {
        return $this->driver;
    }

    public function setHost($host)
    {
        $this->host = $host;
    }

    public function getHost()
    {
        return $this->host;
    }

    public function setPassword($password)
    {
        $this->password = $password;
    }

    public function getPassword()
    {
        return $this->password;
    }

    public function setPort($port)
    {
        $this->port = $port;
    }

    public function getPort()
    {
        return $this->port;
    }

    public function setUser($user)
    {
        $this->user = $user;
    }

    public function getUser()
    {
        return $this->user;
    }
}
